package com.aiti.lulian.service.impl;

import cn.hutool.core.util.ArrayUtil;
import com.aiti.base.core.base.service.iml.BaseSerivceImpl;
import com.aiti.base.core.utils.message.Message;
import com.aiti.base.core.utils.stringUtil.StringUtil;
import com.aiti.lulian.dto.LuLianDockingMeetingSignupInformationDto;
import com.aiti.lulian.entity.LuLianDockingMeetingDetails;
import com.aiti.lulian.entity.LuLianDockingMeetingSignupInformation;
import com.aiti.lulian.mapper.LuLianDockingMeetingDetailsMapper;
import com.aiti.lulian.mapper.LuLianDockingMeetingSignupMapper;
import com.aiti.lulian.service.ILuLianDockingMeetingSignupService;
import com.alibaba.druid.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @Author WongMZ
 * @CreateDate 2024-07-08
 * @Description: LuLianDockingMeetingSignup服务实现类
 */
@Service
public class LuLianDockingMeetingSignupServiceImpl extends BaseSerivceImpl<LuLianDockingMeetingSignupMapper, LuLianDockingMeetingSignupInformation> implements ILuLianDockingMeetingSignupService {
    @Autowired
    private LuLianDockingMeetingSignupMapper dockingMeetingSignupMapper;

    @Autowired
    private LuLianDockingMeetingDetailsMapper detailsMapper;

    @Override
    public int addSignupInformation(LuLianDockingMeetingSignupInformation signupInformation) {

        //获取到要报名的信息
        String meetingId = signupInformation.getMeetingId();
        //获取线下报名的会义信息
        LuLianDockingMeetingDetails meetingDetails = detailsMapper.selectById(meetingId);
        //如果examine值等于1的话，报名状态直接就是通过
        if(StringUtils.equals("1",meetingDetails.getExamine())){
            signupInformation.setStatus("1");
        }
        signupInformation.setBaseCreateTime(new Date(System.currentTimeMillis()));
        return dockingMeetingSignupMapper.insert(signupInformation);
    }

    @Override
    public List<LuLianDockingMeetingDetails> querySignedUpMeetingListPage(Integer pageNum, Integer pageSize, String userId, String activityName) {
        Page<LuLianDockingMeetingSignupInformation> page = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<LuLianDockingMeetingSignupInformation> signUpWrapper = new LambdaQueryWrapper<>();
        signUpWrapper.eq(LuLianDockingMeetingSignupInformation::getCreator, userId);
        Page<LuLianDockingMeetingSignupInformation> signupInformationPage = dockingMeetingSignupMapper.selectPage(page, signUpWrapper);
        if (signupInformationPage.getTotal() > 0) {
            List<LuLianDockingMeetingSignupInformation> signupInformationList = signupInformationPage.getRecords();
            List<String> meetingIds = new ArrayList<>();
            for (LuLianDockingMeetingSignupInformation information : signupInformationList) {
                meetingIds.add(information.getMeetingId());
            }
            List<LuLianDockingMeetingDetails> list = detailsMapper.findList(meetingIds, activityName, userId);

            return list;
        }
        return new ArrayList<>();
    }

    @Override
    public Map<String, Object> queryCount(String userId) {
        Map<String, Object> map = new HashMap<>();

        //已报名数
        LambdaQueryWrapper<LuLianDockingMeetingSignupInformation> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(LuLianDockingMeetingSignupInformation::getCreator, userId);
        Integer signupCount = dockingMeetingSignupMapper.selectCount(queryWrapper);
        //供需对接会活动总数
        Integer MeetingCount = dockingMeetingSignupMapper.selectMeetingCount();
        //待报名的供需对接会活动数
        Integer toBeSignedUpCount = MeetingCount - signupCount;
        //即将开始的供需对接会活动数
        Integer willStartCount = dockingMeetingSignupMapper.selectWillStartCount(userId);

        map.put("signupCount", signupCount);
        map.put("toBeSignedUpCount", toBeSignedUpCount);
        map.put("willStartCount", willStartCount);

        return map;
    }

    /**
     * 查询供应商报名列表
     *
     * @param page
     * @param gongYingShangDto
     * @return
     */
    @Override
    public List<LuLianDockingMeetingSignupInformation> queryGongYingShangBaoMingList(Page<LuLianDockingMeetingSignupInformation> page, LuLianDockingMeetingSignupInformationDto gongYingShangDto,String userId) {
        String roleIds = gongYingShangDto.getRoleIds();
        if(StringUtil.isNotBlank(roleIds)){
            String[] split = roleIds.split(",");
            if(ArrayUtil.containsAny(split,"4ff234fceaa3498aa4058d9dcc130a6b")){
                userId = "";
            }
        }
        List<LuLianDockingMeetingSignupInformation> list = dockingMeetingSignupMapper.findList(page,gongYingShangDto,userId);
        return list;
    }

    @Override
    public Message checkGongYingShang(String id, String status,String bhYy) {
        LuLianDockingMeetingSignupInformation params = new LuLianDockingMeetingSignupInformation();
        params.setBaseId(id);
        params.setStatus(status);
        params.setBhYy(bhYy);
        dockingMeetingSignupMapper.updateById(params);
        return Message.success();
    }

    @Override
    public LuLianDockingMeetingSignupInformation selectInfoById(String id) {
        LuLianDockingMeetingSignupInformation l = dockingMeetingSignupMapper.selectInfoById(id);
        return l;
    }

    @Override
    public int updateSignupInformation(LuLianDockingMeetingSignupInformation signupInformation) {

        signupInformation.setStatus("0");
        //获取到要报名的信息
        String meetingId = signupInformation.getMeetingId();
        //获取线下报名的会议信息
        LuLianDockingMeetingDetails meetingDetails = detailsMapper.selectById(meetingId);
        //如果examine值等于1的话，报名状态直接就是通过
        if(StringUtils.equals("1",meetingDetails.getExamine())){
            signupInformation.setStatus("1");
        }
        signupInformation.setBhYy("");
        return dockingMeetingSignupMapper.updateById(signupInformation);
    }

    @Override
    public List<LuLianDockingMeetingSignupInformation> selectGongYingShangList(String status,String meetingId) {
        List<LuLianDockingMeetingSignupInformation> list = dockingMeetingSignupMapper.selectCustomList(status,meetingId);
        return list;
    }
}